<?php

function reequipment_userapi_getareas($args)
{
    extract($args);

    // Optional arguments
    if (!isset($startnum)) {$startnum = 1;}
    if (!isset($numitems)) {$numitems = -1;}

    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['reareas'];

    $query = "SELECT DISTINCT area_id, area_name"
            . " FROM $table";

    $where = array();
    $bind = array();

    if (!empty($area_id)) {
        $where[] = 'area_id = ?';
        $bind[] = (int)$area_id;
    }

    if (!empty($area_name)) {
        $where[] = 'area_name = ?';
        $bind[] = (string)$area_name;
    }

    if (!empty($where)) {
        $query .= ' WHERE ' . implode(' AND ', $where);
    }

    // Not really necessary
    $query .= " ORDER BY area_id";

    $result =& $dbconn->SelectLimit($query, $numitems, $startnum-1, $bind);
    if (!$result) return;

    $tags = array();
    for (; !$result->EOF; $result->MoveNext()) {
        list($area_id, $area_name) = $result->fields;

        $tags[] = array(
                'area_id'        => $area_id,
                'area_name'      => xarVarPrepForDisplay($area_name)
        );
    }

    $result->Close();
    return $tags;
}
?>